package com.xuchen.zhenyang.controller.sys;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.SysUser;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.enums.StatusEnum;
import com.xuchen.zhenyang.model.UserRoleModel;
import com.xuchen.zhenyang.model.base.LayuiTreeModel;
import com.xuchen.zhenyang.service.SysUserRoleService;
import com.xuchen.zhenyang.service.SysUserService;
import com.xuchen.zhenyang.util.MyUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuchen
 * @since 2018-03-01
 */
@Controller
@RequestMapping("sysUser")
@RequiresPermissions("sysUser")
public class SysUserController extends BaseController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserRoleService sysUserRoleService;
    @Value("${defaultPwd}")
    String defaultPwd;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("defaultPwd",defaultPwd);
        return "sys/user/sys-user-list";
    }

    @GetMapping("list")
    @ResponseBody
    Result list(BaseQuery baseQuery, SysUser myEntity, String params, HttpServletRequest request) {
        if (StrUtil.isNotBlank(params)) {
            myEntity = JSONObject.parseObject(params).toJavaObject(SysUser.class);
        }
        MyEntityWrapper wrapper = new MyEntityWrapper(baseQuery, myEntity);
        wrapper.like("user_name").eq("status");
        List<SysUser> list = sysUserService.selectList(wrapper);
        return Result.success(PageHelper.freeTotal(), list);
    }

    @GetMapping( "toAdd")
    String toAdd(HttpServletRequest request) {
        setAttributeEnums(request);
        return "sys/user/sys-user-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    @RequestLog
    Result doAdd(SysUser myEntity, HttpServletRequest request) {
        myEntity.setCreateTime(new Date());
        myEntity.setCreateUser(getSessionUserId());
        MyUtils.encrypPassword(myEntity);
        sysUserService.insert(myEntity);
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(SysUser myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", sysUserService.selectById(myEntity));
        return "sys/user/sys-user-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    @RequestLog
    Result doEdit(SysUser myEntity, HttpServletRequest request) {
        if (myEntity.getStatus() == null) {
            myEntity.setStatus(0);
        }
        sysUserService.updateById(myEntity);
        return Result.success();
    }

    @GetMapping("toUserRole")
    String toRoleMenu(Integer id, HttpServletRequest request) {
        request.setAttribute("userId", id);
        return "sys/user/sys-user-role";
    }

    @GetMapping("userRoleTree")
    @ResponseBody
    String userRoleTree(Integer paramId) {
        List<LayuiTreeModel> list = new ArrayList<>();
        List<UserRoleModel> userRoleList = sysUserRoleService.getUseRoleTreeByUserId(paramId);
        for (UserRoleModel model : userRoleList) {
            LayuiTreeModel treeParModel = new LayuiTreeModel();
            treeParModel.setValue(model.getRoleId());
            treeParModel.setTitle(model.getRoleName());
            treeParModel.setChecked(model.getUserId()!=null);
            list.add(treeParModel);
        }
        return JSONArray.toJSONString(list);
    }

    @PostMapping("updateUserRole")
    @ResponseBody
    Result updateUserRole(Integer id, Integer[] ids) {
        logger.info("id=" + id + ",ids=" + Arrays.toString(ids));
        sysUserRoleService.updateUserRole(id, ids);
        return Result.success();
    }

    @PostMapping("resetPwd")
    @ResponseBody
    @RequestLog
    Result resetPwd(SysUser myEntity) {
        myEntity.setPassword(defaultPwd);
        MyUtils.encrypPassword(myEntity);
        sysUserService.updateById(myEntity);
        return Result.success();
    }

    private void setAttributeEnums(HttpServletRequest request) {
        request.setAttribute("userStatus", StatusEnum.getMap());
    }
}

